package eureka.ui.me;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eureka.domain.account.WebsiteUser;
import eureka.exceptions.DatabaseException;
import eureka.service.account.IAccountManagementService;
import eureka.service.account.IAccountService;
import eureka.service.account.impl.AccountManagementService;
import eureka.service.account.impl.AccountService;

/**
 * Servlet implementation class ChangeMyInfo
 */
@WebServlet("/Me/ChangeMyInfo")
public class ChangeMyInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IAccountService acountService = new AccountService();
	private IAccountManagementService ams = new AccountManagementService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeMyInfo() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		if (session.getAttribute("user") == null) {
			response.sendError(403);
			return;
		}
		
		request.getRequestDispatcher("changemyinfo.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		WebsiteUser user = (WebsiteUser)session.getAttribute("user");
		if (user == null) {
			response.sendError(403);
			return;
		}
		
		String nickName = request.getParameter("nickName");
		String phoneNumber = request.getParameter("phoneNumber");
		String signature = request.getParameter("signature");
		
		if (nickName == null || 
				phoneNumber == null || 
				signature == null) {
			doGet(request, response);
			return;
		}
		
		try {
			user = ams.getWebsiteUserByUID(user.getUID());
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		if (user == null) {
			response.sendError(403);
			return;
		}
		
		boolean isNickNameExisted = false;
		try {
			isNickNameExisted = acountService.checkNickNameExistence(nickName, user.getUID());
		} catch (IllegalArgumentException e1) {
			response.sendError(500);
			return;
		} catch (DatabaseException e1) {
			response.sendError(500);
			return;
		}
		
		if (isNickNameExisted) {
			request.setAttribute("result", -1);
			request.getRequestDispatcher("changemyinfo.jsp").forward(request, response);
			return;
		}
		
		user.setNickName(nickName);
		user.setPhoneNumber(phoneNumber);
		user.setSignature(signature);
		
		try {
			ams.updateWebsiteUser(user);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		session.setAttribute("user", user);
		request.setAttribute("result", 1);
		request.getRequestDispatcher("changemyinfo.jsp").forward(request, response);
	}

}
